package com.lechuang.xiaoxiao.view.activity.own;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.lechuang.xiaoxiao.R;
import com.lechuang.xiaoxiao.model.bean.OwnMyAgentBean;
import com.lechuang.xiaoxiao.presenter.CommonAdapter;
import com.lechuang.xiaoxiao.presenter.net.Netword;
import com.lechuang.xiaoxiao.presenter.net.ResultBack;
import com.lechuang.xiaoxiao.presenter.net.netApi.OwnApi;
import com.lechuang.xiaoxiao.view.defineView.MListView;
import com.lechuang.xiaoxiao.view.defineView.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 作者：li on 2017/10/6 15:42
 * 邮箱：961567115@qq.com
 * 修改备注:
 */
public class MyTeamActivity extends AppCompatActivity {
    @BindView(R.id.refresh_item)
    PullToRefreshScrollView refreshItem;
    private MListView lv_team;
    private Context mContext = MyTeamActivity.this;
    public int page = 1;
    List<OwnMyAgentBean.RecordBean.ListBean> items = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_team);
        ButterKnife.bind(this);
        initView();
        getData();
    }

    public void initView() {
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ((TextView) findViewById(R.id.tv_title)).setText("我的团队");
        lv_team = (MListView) findViewById(R.id.lv_team);
        refreshItem.setOnRefreshListener(refresh);
        refreshItem.setMode(PullToRefreshBase.Mode.BOTH);

    }

    public void getData() {
        Netword.getInstance().getApi(OwnApi.class)
                .agentInfo(page)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResultBack<OwnMyAgentBean>(mContext) {

                    @Override
                    public void successed(OwnMyAgentBean result) {
                        //下级数据
                        List<OwnMyAgentBean.RecordBean.ListBean> list = result.record.list;
                        if (list != null && list.size() > 0) {
                            int size = list.size();
                            for (int i = 0; i < size; i++) {
                                items.add(list.get(i));
                            }
                            lv_team.setAdapter(new CommonAdapter<OwnMyAgentBean.RecordBean.ListBean>(mContext, items, R.layout.myteam_item) {
                                @Override
                                public void setData(ViewHolder viewHolder, Object item) {
                                    if (((OwnMyAgentBean.RecordBean.ListBean) item).photo != null)
                                        viewHolder.displayImage(R.id.iv_img, ((OwnMyAgentBean.RecordBean.ListBean) item).photo);
                                    //昵称
                                    viewHolder.setText(R.id.tv_name, ((OwnMyAgentBean.RecordBean.ListBean) item).nickname);
                                    //个人收入
                                    viewHolder.setText(R.id.tv_nextNum, "下级人数:" + ((OwnMyAgentBean.RecordBean.ListBean) item).nextAgentCount);
                                    //加入时间
                                    viewHolder.setText(R.id.tv_time, "加入时间:" + ((OwnMyAgentBean.RecordBean.ListBean) item).joinTime);
                                }
                            });
                        }
                    }
                });
    }

    private PullToRefreshBase.OnRefreshListener2 refresh = new PullToRefreshBase.OnRefreshListener2() {
        @Override
        public void onPullDownToRefresh(PullToRefreshBase refreshView) {
            String label = DateUtils.formatDateTime(
                    mContext.getApplicationContext(),
                    System.currentTimeMillis(),
                    DateUtils.FORMAT_SHOW_TIME
                            | DateUtils.FORMAT_SHOW_DATE
                            | DateUtils.FORMAT_ABBREV_ALL);
            // 显示最后更新的时间
            refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
            page = 1;
            if (null != items) {
                items.clear();
            }
            //refreshScrollView.getRefreshableView().smoothScrollTo(0, 0);
            // 模拟加载任务
            getData();
            refreshView.onRefreshComplete();
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase refreshView) {
            String label = DateUtils.formatDateTime(
                    mContext.getApplicationContext(),
                    System.currentTimeMillis(),
                    DateUtils.FORMAT_SHOW_TIME
                            | DateUtils.FORMAT_SHOW_DATE
                            | DateUtils.FORMAT_ABBREV_ALL);
            // 显示最后更新的时间
            refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
            page += 1;
            // 模拟加载任务
            getData();
            refreshView.onRefreshComplete();
        }
    };
}
